package com.cs.beneficios.common.util;

import com.cs.beneficios.business.entity.VflexDAO;

import com.cs.beneficios.business.model.VflexVO;
import com.cs.beneficios.common.ConnectionFactory;

import java.sql.SQLException;

import java.util.ArrayList;
//import java.util.Collection;
import java.util.Iterator;

public class VisualHelper {

    public String getZonas(){
        //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
		ConnectionFactory cf = new ConnectionFactory();
        VflexDAO valorFlexDAO = new VflexDAO(cf);
        ArrayList zonas = new ArrayList();
        try {
            zonas = (ArrayList)valorFlexDAO.getVflex("ZONA");
            cf.closeConnection(valorFlexDAO.getConn());
        } catch (SQLException e) {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
        }
        
        return collectionToSelect(zonas);
    }
    
    public String getComunas(){
        //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
		ConnectionFactory cf = new ConnectionFactory();
        VflexDAO valorFlexDAO = new VflexDAO(cf);
        ArrayList comunas = new ArrayList();
        try {
            comunas = (ArrayList)valorFlexDAO.getVflex("COMUNA");
            cf.closeConnection(valorFlexDAO.getConn());
        } catch (SQLException e) {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
        }
        return collectionToSelect(comunas);
    }
    
    public String getBarrios(){    
        //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
		ConnectionFactory cf = new ConnectionFactory();
        VflexDAO valorFlexDAO = new VflexDAO(cf);
        ArrayList barrios = new ArrayList();
        try {            
            barrios = (ArrayList)valorFlexDAO.getVflex("BARRIO");
            cf.closeConnection(valorFlexDAO.getConn());
        } catch (SQLException e) {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
        }
        return collectionToSelect(barrios);
    }
    
    public String buscarPadre(String code){
        //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
		ConnectionFactory cf = new ConnectionFactory();
        VflexDAO valorFlexDAO = new VflexDAO(cf);
        String padre = "";
        try {            
            padre = valorFlexDAO.buscarPadre(code);
            padre += "|"+valorFlexDAO.buscarPadre(padre);
            cf.closeConnection(valorFlexDAO.getConn());
        } catch (SQLException e) {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
        }
        return padre;
        
    }
    
    private String collectionToSelect(ArrayList col){
        String select = "[ { name: 'Seleccione una opcion', id: '0'}, ";
        if(col != null && col.size() > 0){
            for(Iterator i = col.iterator();i.hasNext();){
                VflexVO valorFlex = (VflexVO)i.next();
                select += "{ name:'"+valorFlex.getDescVflex()+"', id:'"+valorFlex.getCode()+"'},";
            }
            
        }
        select += "{}]";
        return select;
    }
    
}
